﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 线程池
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("----------------开始a-------------------------");
            MyTask a = new MyTask(2);
            for (int i = 0; i < 5; i++)
            {
                var n = i;
                a.Run(() =>
                {
                    Console.WriteLine(n);
                    Thread.Sleep(1000);
                });
            }
            //掉用下面任意一个后 任务队列不再接收任何任务
            a.Dispose();//收到结束指令后 不再继续执行 当前正在任务执行完毕 即可退出 
            Console.WriteLine("----------------结束a-------------------------");
            Thread.Sleep(2000);
            Console.WriteLine("----------------开始b-------------------------");
            MyTask b = new MyTask(2);
            for (int i = 0; i < 5; i++)
            {
                var n = i;
                b.Run(() =>
                {
                    Thread.Sleep(1000);
                    Console.WriteLine(n);
                });
            }
            //掉用下面任意一个后 任务队列不再接收任何任务
            b.Dispose(true);//收到指令后 继续执行 直到队列任所有务执行完毕后 即可退出
            Console.WriteLine("----------------结束b-------------------------");
            Thread.Sleep(2000);
            Console.WriteLine("----------------开始c-------------------------");
            using (MyTask c = new MyTask(2,false))
            {
                for (int i = 0; i < 5; i++)
                {
                    var n = i;
                    c.Run(() =>
                    {
                        Console.WriteLine(n);
                        Thread.Sleep(1000);
                    });
                }
            }

            Console.WriteLine("----------------结束c-------------------------");
            Thread.Sleep(2000);
            Console.WriteLine("----------------开始d-------------------------");
            using (MyTask d = new MyTask(2, true))
            {
                for (int i = 0; i < 5; i++)
                {
                    var n = i;
                    d.Run(() =>
                    {
                        Console.WriteLine(n);
                        Thread.Sleep(1000);
                    });
                }
            }

            Console.WriteLine("----------------结束d-------------------------");
            Console.Read();

        }
    }
}
